class GunPanel extends eui.Group {
    private pwidth: number;
    private pheight: number;
    private sybmArr = [];
    private oSybmArr = [];
    private gunArr = [];
    private gunId = 0;
    private context: any;
    private postData: any;
    private call: any;
    public gameWay = "speed";  //默认为快速玩法
    public gameSpeedSigal = 0;
    public ccBorderArr = [];
    public gunCtr = [];
    public gunAm: GunAm;
    public rotationSpeed:number = 55;
    
    constructor(width: number, height: number, data: any, context: any) {
        super();
        this.context = context;
        this.pwidth = width;
        this.pheight = height;

        this.sybmArr['NI'] = "NI";
        this.sybmArr['KK'] = "KK";
        this.sybmArr['JJ'] = "JJ";
        this.sybmArr['QQ'] = "QQ";
        this.sybmArr['TN'] = "TN";
        this.sybmArr['AA'] = "AA";
        this.sybmArr['C1'] = "C1";
        this.sybmArr['WW'] = "WW";
        this.sybmArr['M1'] = "M1";
        this.sybmArr['M2'] = "M2";
        this.sybmArr['M3'] = "M3";
        this.sybmArr['M4'] = "M4";
        this.sybmArr['M5'] = "M5";
        var sybm = data.Value.Symbolstripes.Main.Stripes;
        var initsybm = data.Value.Geninit.Main.ReelResults;
        this.createScene(sybm, initsybm);
    }
    public setGameWay(str: string): void {
        this.gameWay = str;
    }
    protected createScene(sybm: any, initsybm: any): void {
        super.createChildren();
        var btn: eui.Button = new eui.Button();
        btn.label = "Button";
        var image = new eui.Image();
        image.source = "gun_panle_png";
       // this.pwidth = 830;
        image.width = this.pwidth;
        image.height = this.pwidth * 0.46;
        image.x = 20;
        image.alpha = 0;
        this.addChild(image);
        this.scrollEnabled = true;
        let pos = [0,0,0,15,15];
        for (var i = 0; i < 5; i++) {
            var gunOne = new GunOne(this.switchSybm(sybm[i].Symbols), initsybm[i], this, this.context);
            gunOne.x = this.pwidth / 5 * i + pos[i];
            gunOne.y = 15;
            this.gunArr.push(gunOne);
            this.addChild(gunOne);
        }

        // this.gunArr[1].x += 5;

        // this.gunArr[3].x += 10;
        // this.gunArr[4].x += 5;


        for (var i = 0; i < 5; i++) {
            var test = this.pwidth / 5;
            var shp: egret.Shape = new egret.Shape();
            shp.graphics.lineStyle(3, 0x00ff00);
            shp.graphics.moveTo(10 + i * test, 10);
            shp.graphics.lineTo(160 + i * test, 10);
            shp.graphics.lineTo(160 + i * test, 350);
            shp.graphics.lineTo(10 + i * test, 350);
            shp.graphics.lineTo(10 + i * test, 10);
            shp.graphics.endFill();
            this.ccBorderArr.push(shp);
            //this.addChild(shp);
        }
        this.ccBorderArr[1].x += 5;
        this.ccBorderArr[2].x += 5;
        this.ccBorderArr[3].x += 10;
        this.ccBorderArr[4].x += 15;
        // this.ccBorderArr[1].x +=5; 
    }

    public start(call: any): void {
        var point = this;
        this.call = call;
        for (var i = 0; i < point.gunArr.length; i++) {
            point.gunArr[i].start(200 * i);
        }
    }
    /**
     * 设置回传数据
     */
    public setData(data: any) {
        var postData = data.Value.SpinResult.Main.ReelResults;
        var postSys = data.Value.SpinResult.Main.ReelSymbols;
        var _Sym = [];
        var C1Num = 0;

        /**
         * 获取每个C1的数量
         */
        for (var i = 0; i < 5; i++) {
            _Sym[i] = ishasC1(postSys[i]);
            // if(ishasC1(postSys[i])){
            //     C1Num++;
            // }
        }

//      console.log("获得免费游戏标志", _Sym);

        for (var i = 0; i < 4; i++) {
            // _Sym[i] = ishasC1(postSys[i]);
            if (ishasC1(postSys[i])) {
                C1Num++;
            }
        }

        if (C1Num < 2) {
            for (var i = 0; i < 5; i++) {
                // _Sym[i] = ishasC1(postSys[i]);
                // if(ishasC1(postSys[i])){
                //     C1Num++;
                // }
                this.gunCtr[i] = 1;
            }
        } else {

            this.gunCtr[0] = 1;
            this.gunCtr[1] = 1;

            // 判断这个3框是否要

            if (_Sym[0] && _Sym[1]) {
                if (_Sym[2]) {
                    this.gunCtr[2] = 3;
                } else {
                    this.gunCtr[2] = 2;
                }
            } else {
                this.gunCtr[2] = 1;
            }

            this.gunCtr[3] = getC1Ctr.call(this, 3);
            this.gunCtr[4] = getC1Ctr.call(this, 4);

        }

        //console.log(this.gunCtr);

        /**
         * 传递定位的符号位
         */
        for (var i = 0; i < this.gunArr.length; i++) {
            this.gunArr[i].setData(postData[i]["Pos"]);
        }
        this.gunId = 0;
        this.gunArr[this.gunId].setGunCtr(this.gunCtr[this.gunId]);

        if (this.gunCtr[this.gunId] > 1) {
            this.addChild(this.ccBorderArr[this.gunId]);
        }

        /**
         * 判断是否有C1
         */
        function ishasC1(Arr) {
            for (var i = 0; i < Arr.length; i++) {
                if (Arr[i] == "C1") {
                    return true;
                }
            }
            return false;
        }

        function getC1Ctr(num) {
            if (this.gunCtr[num - 1] == 3) {
                if (_Sym[num]) {
                    return 3;
                } else {
                    return 2;
                }
            } else {
                return 1;
            }
        }
    }
    /**
     * 转换相关的符号
     * @param sybm
     * @returns {Array}
     */
    protected switchSybm(sybm: any): any {
        var sybmArr = [];
        for (var i = 0; i < sybm.length; i++) {
            var on = [];
            on['symbol'] = sybm[i];
            on['text'] = this.sybmArr[sybm[i]];
            sybmArr.push(on);
        }
        return sybmArr;


    }

    /**
     * 用于子界面完成后的回调函数
     */
    public conpleteGun(): void {
        if (this.gunId > 3) {
            //this.completeScence(this.postData);

            for (var i = 0; i < 5; i++) {
                if (this.contains(this.ccBorderArr[i])) {
                    this.removeChild(this.ccBorderArr[i]);
                }
            }
            this.call.completeRotate();
            return;
        } else {

            if (this.gunCtr[this.gunId] < 3) {
                if (this.contains(this.ccBorderArr[this.gunId])) {
                    this.removeChild(this.ccBorderArr[this.gunId]);
                }
            }

            this.gunId = this.gunId + 1;
            this.gunArr[this.gunId].setGunCtr(this.gunCtr[this.gunId]);

            if (this.gunCtr[this.gunId] > 1) {
                this.addChild(this.ccBorderArr[this.gunId]);
            }

        }
    }

    /**
     * 打开动画
     */
    public openAm(reArr: any, symArr: any): void {
        for (var i = 0; i < reArr.length; i++) {
            this.gunArr[i].openAm(reArr[i]);
        }
        this.gunAm = new GunAm(reArr, symArr);
        this.addChild(this.gunAm);
    }

    /**
     * 关闭动画
     */
    public closeAm(): void {
        for (var i = 0; i < 5; i++) {
            this.gunArr[i].closeAm();
        }

        this.removeChild(this.gunAm);
    }

}
